#define _CRT_SECURE_NO_WARNINGS 1

#include"ListQueue.h"

void InitQueue(ListQueue** q)
{
	ListQueue* newqueue = (ListQueue*)malloc(sizeof(ListQueue));
	if (newqueue == NULL)
	{
		perror("newqueue malloc:");
		*q = NULL;
		return;
	}
	newqueue->front = newqueue->rear = NULL;
	*q = newqueue;
}

void enQueue(ListQueue* q, ElemType e)
{
	assert(q);
	LQNode* newnode = (LQNode*)malloc(sizeof(LQNode));
	if (newnode == NULL)
	{
		perror("newnode malloc:");
		return;
	}
	newnode->data = e;
	newnode->next = NULL;
	if (q->front == NULL)
	{
		q->front = q->rear = newnode;
	}
	else 
	{
		q->rear->next = newnode;
		q->rear = q->rear->next;
	}
}

bool QueueEmpty(ListQueue* q)
{
	assert(q);
	return q->rear == NULL;
}

bool deQueue(ListQueue* q, ElemType* e)
{
	assert(q);
	if (q->rear == NULL)
	{
		return false;
	}
	*e = q->front->data;
	LQNode* cur = q->front;
	if (q->front == q->rear)
	{
		q->front = q->rear = NULL;
	}
	else
	{
		q->front = q->front->next;
	}
	free(cur);
	return true;
}

void DestroyQueue(ListQueue* q)
{
	assert(q);
	if (q->rear != NULL)
	{
		LQNode* cur = q->front;
		while (cur != NULL)
		{
			q->front = q->front->next;
			free(cur);
			cur = q->front;
		}
	}
	free(q);
}